﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShortcutMenu
{
    public class Disposition
    {
        public static Disposition dispositionApp;

        private string FilePath = Directory.GetCurrentDirectory();

        private string FileName = @"Disposition.json";

        private string path;

        public Disposition()
        {
            path = this.FilePath + '/' + this.FileName;
            if (!IsExist())
                CreateDisposition();
            dispositionApp = this;
        }

        private bool IsExist()
        {
            return File.Exists(path);
        }

        private void CreateDisposition()
        {
            File.Create(path).Close();
            WriteDispoition(new DispositionFile());
        }

        public DispositionFile LoadDisposition()
        {
            DispositionFile disposition;

            using (StreamReader sr = new StreamReader(path))
            {
                var json = sr.ReadToEnd();
                disposition = JsonConvert.DeserializeObject<DispositionFile>(json)!;
            }

            if (disposition == null)
            {
                disposition = new();
            }
            return disposition;
        }

        public void WriteDispoition(DispositionFile disposition)
        {
            var json = JsonConvert.SerializeObject(disposition);
            using (StreamWriter sw = new(path))
            {
                sw.Write(json);
            }
        }

    }
}
